package com.smart.translator.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.smart.translator.R;
import com.smart.translator.data.model.Language;
import java.util.List;

public class LanguageAdapter extends ArrayAdapter<Language> {
    private final Context context;
    private final List<Language> languages;

    public LanguageAdapter(@NonNull Context context, @NonNull List<Language> languages) {
        super(context, android.R.layout.simple_spinner_item, languages);
        this.context = context;
        this.languages = languages;
        setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return createItemView(position, convertView, parent);
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return createItemView(position, convertView, parent);
    }

    private View createItemView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(android.R.layout.simple_spinner_item, parent, false);
        }

        TextView textView = convertView.findViewById(android.R.id.text1);
        Language language = languages.get(position);
        textView.setText(language.getName());

        return convertView;
    }
} 